package transaction.mq.learning.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;
import transaction.mq.entity.Result;
import transaction.mq.entity.StatusCode;
import transaction.mq.exception.ExceptionCast;
import transaction.mq.learning.mapper.XcLearningCourseMapper;
import transaction.mq.learning.mapper.XcTaskHisMapper;
import transaction.mq.learning.model.XcLearningCourse;
import transaction.mq.learning.model.XcTaskHis;
import transaction.mq.order.model.XcTask;
import transaction.mq.util.IdWorker;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 **/
@Service
@Slf4j
public class LearningService {

    @Autowired
    XcLearningCourseMapper xcLearningCourseMapper;

    @Autowired
    XcTaskHisMapper xcTaskHisMapper;

    private IdWorker idWorker = new IdWorker(1, 0);

    //添加选课
    @Transactional(rollbackFor = Exception.class)
    public Result addCourse(String userId, String courseId, String valid, Date startTime, Date endTime, XcTask xcTask) {
        if (StringUtils.isEmpty(courseId)) {
            ExceptionCast.cast(StatusCode.ERROR);
        }
//        if (StringUtils.isEmpty(userId)) {
//            ExceptionCast.cast(LearningCode.CHOOSECOURSE_USERISNULl);
//        }
//        if(xcTask == null || StringUtils.isEmpty(xcTask.getId())){
//            ExceptionCast.cast(LearningCode.CHOOSECOURSE_TASKISNULL);
//        }
        // 查询
        Example example = new Example(XcLearningCourse.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("courseId", courseId);
        List<XcLearningCourse> list = xcLearningCourseMapper.selectByExample(example);
        XcLearningCourse xcLearningCourse = CollectionUtils.isEmpty(list) ? null : list.get(0);

        if (xcLearningCourse != null) {
            log.error("已经获取到重复消息：{}", xcLearningCourse.toString());
            //更新选课记录
            //课程的开始时间
            xcLearningCourse.setStartTime(startTime);
            xcLearningCourse.setEndTime(endTime);
            xcLearningCourse.setStatus("501001");
            xcLearningCourseMapper.updateByPrimaryKeySelective(xcLearningCourse);
        } else {
            //添加新的选课记录
            xcLearningCourse = new XcLearningCourse();
            xcLearningCourse.setId(idWorker.nextId() + "");
            xcLearningCourse.setUserId(userId);
            xcLearningCourse.setCourseId(courseId);
            xcLearningCourse.setStartTime(startTime);
            xcLearningCourse.setEndTime(endTime);
            xcLearningCourse.setStatus("501001");
            xcLearningCourseMapper.insertSelective(xcLearningCourse);
        }
        //向历史任务表播入记录
        XcTaskHis optional = xcTaskHisMapper.selectByPrimaryKey(xcTask.getId());
        if (optional == null) {
            //添加历史任务
            XcTaskHis xcTaskHis = new XcTaskHis();
            BeanUtils.copyProperties(xcTask, xcTaskHis);
            xcTaskHisMapper.insertSelective(xcTaskHis);
        }
        return new Result();
    }
}
